package bjut.zrpc.core.optimization.channel;

import io.netty.channel.Channel;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: zhuzhiming
 * @Date: 2022/6/24 17:28
 */
public class ChannelRecord {

    private static final ConcurrentHashMap<String, Channel> CHANNEL_MAP = new ConcurrentHashMap<>();

    public void putChannel(String inetSocketAddress, Channel channel) {
        CHANNEL_MAP.put(inetSocketAddress, channel);
    }

    public Channel getChannel(String inetSocketAddress) {

        Channel channel = CHANNEL_MAP.get(inetSocketAddress);

        if (channel != null && channel.isActive()) {
            return channel;
        } else {
            CHANNEL_MAP.remove(inetSocketAddress);
        }
        return null;
    }


}
